/**
 * 
 */

//*********************//
/*jslint maxerr: 50, indent: 4, browser: true */
(function () {
    //"use strict";

    function walkTheDOM(node, func) {
        func(node);
        node = node.firstChild;
        while (node) {
            walkTheDOM(node, func);
            node = node.nextSibling;
        }
    }

    function getTextNodes(element) {
        var nodes = [];

        walkTheDOM(element, function (node) {
            if (node.nodeType === 3) {
                nodes.push(node);
            }
        });

        return nodes;
    }

    function escapeRegex(string) {
        return string.replace(/[\[\](){}?*+\^$\\.|]/g, "\\$&");
        return string;
    }

    function highlight(element, string, classname) {
        var nodes = getTextNodes(element),
            length = nodes.length,
            stringLength = string.length,
            rx = new RegExp("\\B" + escapeRegex(string)),
            i = 0,
            index,
            text,
            newContent,
            span,
            node;

        while (i < length) {
            node = nodes[i];
            newContent = document.createDocumentFragment();
            text = node.nodeValue;
            index = text.search(rx);
            while (index !== -1) {
                newContent.appendChild(document.createTextNode(text.slice(0, index)));
                text = text.slice(index + stringLength);
                span = document.createElement("span");
                span.className = classname;
                span.appendChild(document.createTextNode(string));
                newContent.appendChild(span);
                index = text.search(rx);
            }

            newContent.appendChild(document.createTextNode(text));
            node.parentNode.replaceChild(newContent, node);
            i += 1;
        }
    }

    function addEvent(elem, event, fn) {
        if (typeof elem === "string") {
            elem = document.getElementById(elem);
        }

        function listenHandler(e) {
            var ret = fn.apply(null, arguments);

            if (ret === false) {
                e.stopPropagation();
                e.preventDefault();
            }

            return ret;
        }

        function attachHandler() {
            window.event.target = window.event.srcElement;

            var ret = fn.call(elem, window.event);

            if (ret === false) {
                window.event.returnValue = false;
                window.event.cancelBubble = true;
            }

            return ret;
        }

        if (elem.addEventListener) {
            elem.addEventListener(event, listenHandler, false);
        } else {
            elem.attachEvent("on" + event, attachHandler);
        }
    }

    function emptyNode(node) {
        while (node.firstChild) {
            node.removeChild(node.firstChild);
        }
    }

    function toPreviewHighlight(e, to) {
        if (typeof to === "string") {
            to = document.getElementById(to);
        }

        var value = e.target.value,
            tags = value.match(/\B@([^\u0000-\u007F]|[\w])+/g) || [],
            index = tags.length - 1,
            lookup = {},
            fragment,
            length,
            tag;

        while (index >= 0) {
            tag = tags[index];
            if (!tag.length || tag === "@" || tag.charAt(0) !== "@" || lookup[tag]) {
                tags.splice(index, 1);
            } else {
                lookup[tag] = true;
            }
            index -= 1;
        }

        fragment = document.createDocumentFragment();
        fragment.appendChild(document.createTextNode(value));
        index = 0;
        length = tags.length;
        while (index < length) {
            tag = tags[index];
            highlight(fragment, tag, "hashSymbol");
            index += 1;
        }
        emptyNode(to);
        to.appendChild(fragment);
        to.innerHTML = to.innerHTML.replace(/\n/g, "<br />");
    }

    addEvent("userInput", "keyup", function (e) {
        toPreviewHighlight(e, "preview");
    });
}());